<!-- RoleList 管理员角色列表 -->
<template>
  <div class="content">
    <el-form :model="query" ref="searchForm" size="medium" class="search-form">
      <el-form-item prop="name">
        <el-input v-model="query.name" placeholder="请输入角色名" @keyup.enter.native="handleSearch"></el-input>
      </el-form-item>
      <el-form-item label-width="10px">
        <Cbutton @click="handleSearch" size="medium" :btnloading="tableLoading">查询</Cbutton>
        <Cbutton type="info" size="medium" @click="handleReset('searchForm')">重置</Cbutton>
      </el-form-item>
    </el-form>

    <div class="content-container">
      <div class="content-btn">
        <Cbutton @click="handleAdd">创建角色</Cbutton>
      </div>
      <el-table :data="tableList" v-loading="tableLoading" ref="tableData" border fit>
        <el-table-column label="序号" type="index" align="center" width="70"></el-table-column>
        <el-table-column prop="name" label="角色名" align="center" min-width="100"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.createTime | glbTimeFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" min-width="120" align="center">
          <template slot-scope="scope">
            <Cbutton type="text" size="medium" @click="handleEdit(scope.row)">编辑</Cbutton>
          </template>
        </el-table-column>
      </el-table>

      <Pagination @pageChange="getPage" :pageDefultData="{
        pageNum: query.pageNum,
        pageRow: query.pageSize,
        totalCount: query.totalCount }">
      </Pagination>
    </div>

    <!-- 创建角色 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.mVisible" @close="handleCancle" v-if="dialog.mVisible" width="500px"
               class="dialog-form">
      <el-form :model="dialog.data" :rules="dialog.rules" ref="DlalogForm" size="medium" label-width="100px">
        <el-form-item label="角色名称：" prop="name">
          <el-input v-model="dialog.data.name" placeholder="请输入角色名称" @change="handleChangeRoleName"></el-input>
        </el-form-item>
        <el-form-item label="功能权限：" v-if="dialog.oprType === 'EDIT'">
          <permission :roleId="dialog.data.id" @getPerData="getPerData"></permission>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="handleCancle">取 消</el-button>
        <el-button size="medium" :loading="dialog.loading" type="primary" @click="handleDiaSub('DlalogForm')">确
          定</el-button>
      </span>
    </el-dialog>
    <!-- 创建角色 -->

  </div>
</template>

<script>
import { GetRoleList, AddRole, EditRole, SetRolePermission } from '@/api/system'
import { RoleInfoRules } from '@/utils/rules'
import permission from './permission'
export default {
  name: 'RoleList',
  components: { permission },
  data () {
    return {
      query: {
        name: '',
        pageNum: 1,
        pageSize: 10,
        totalCount: 0
      },
      tableList: [],
      tableLoading: false,
      permissionsList: [
        { value: '1', label: '管理员权限' },
        { value: '0', label: '普通用户权限' }
      ],
      dialog: {
        title: '',
        mVisible: false,
        loading: false,
        oprType: 'ADD',
        data: {
          name: ''
        },
        permissionIds: [],
        rules: RoleInfoRules
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    getList () {
      this.tableLoading = true
      let params = {
        pageSize: this.query.pageSize,
        pageNum: this.query.pageNum,
        name: this.query.name
      }
      GetRoleList(params).then(res => {
        this.tableLoading = false
        const data = res.data
        this.tableList = data.list
        this.query.totalCount = data.total
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleSearch () {
      this.query.pageNum = 1
      this.getList()
    },
    handleReset (formName) {
      this.$refs[formName].resetFields()
    },
    handleEdit (row) {
      this.dialog.title = '修改'
      this.dialog.mVisible = true
      this.dialog.oprType = 'EDIT'
      this.dialog.data.id = row.id
      for (const key in this.dialog.data) {
        if (key === 'roleId') {
          this.dialog.data.roleId = row.roleId.toString()
        } else {
          this.dialog.data[key] = row[key]
        }
      }
    },
    handleAdd () {
      this.dialog.title = '创建'
      this.dialog.mVisible = true
      this.dialog.oprType = 'ADD'
    },
    handleCancle () {
      this.dialog.mVisible = false
      for (const key in this.dialog.data) {
        this.dialog.data[key] = ''
      }
    },
    handleDiaSub (formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return false
        this.dialog.loading = true
        if (this.dialog.oprType === 'ADD') {
          const params = JSON.parse(JSON.stringify(this.dialog.data))
          AddRole(params).then(res => {
            this.$message.success(res.message || '操作成功')
            this.getList()
            this.dialog.loading = false
            this.dialog.mVisible = false
          }).catch(() => {
            this.dialog.loading = false
          })
        }
        if (this.dialog.oprType === 'EDIT') {
          this.EditRolePermissionReq({
            roleId: this.dialog.data.id,
            permissionIds: this.dialog.permissionIds
          })
        }
      })
    },

    handleChangeRoleName () {
      if (this.dialog.oprType === 'EDIT') {
        const parmas = {
          id: this.dialog.data.id,
          name: this.dialog.data.name
        }
        EditRole(parmas).then(res => {
          this.getList()
        }).catch(() => { })
      }
    },

    EditRolePermissionReq (params) {
      SetRolePermission(params).then(res => {
        this.$message.success(res.message || '操作成功')
        this.dialog.mVisible = false
        this.dialog.loading = false
      }).catch(() => {
        this.dialog.loading = false
      })
    },

    getPage (val) {
      this.query.pageNum = val.pageNum
      this.query.pageSize = val.pageRow
      this.getList()
    },
    getPerData (val) {
      this.dialog.permissionIds = val
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
